﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmDsUser : FrmBaseListForm
    {
        public FrmDsUser()
        {
            InitializeComponent();
        }

        public override void InitGridColumns()
        {
            DevExpress.XtraGrid.Columns.GridColumn colAccountName = new DevExpress.XtraGrid.Columns.GridColumn();
            colAccountName.Caption = "Account Name";
            colAccountName.FieldName = "AccountName";
            colAccountName.Name = "colID";
            colAccountName.Visible = true;
            colAccountName.Width = 100;
            colAccountName.MinWidth = 100;
            colAccountName.OptionsColumn.FixedWidth = true;
            colAccountName.VisibleIndex = 0;

            DevExpress.XtraGrid.Columns.GridColumn colRoleID = new DevExpress.XtraGrid.Columns.GridColumn();
            colRoleID.Caption = "Role";
            colRoleID.FieldName = "AccessAuthorityID";
            colRoleID.Name = "colName";
            colRoleID.Visible = true;
            colRoleID.Width = 100;
            colRoleID.MinWidth = 100;
            colRoleID.OptionsColumn.FixedWidth = true;
            colRoleID.VisibleIndex = 1;

            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit rRole = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();
            rRole.DataSource = DB.AccessAuthorities.ToList();
            rRole.ValueMember = "AccessAuthorityID";
            rRole.DisplayMember = "AccessAuthorityName";
            rRole.NullText = string.Empty;
            gridDSDoiTuong.RepositoryItems.Add(rRole);
            colRoleID.ColumnEdit = rRole;


            DevExpress.XtraGrid.Columns.GridColumn colStaffID = new DevExpress.XtraGrid.Columns.GridColumn();
            colStaffID.Caption = "Staff";
            colStaffID.FieldName = "StaffID";
            colStaffID.Name = "colStaffID";
            colStaffID.Visible = true;
            colStaffID.Width = 100;
            colStaffID.MinWidth = 100;
            colStaffID.OptionsColumn.FixedWidth = true;
            colStaffID.VisibleIndex = 2;

            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit rStaff = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();
            rStaff.DataSource = DB.Staffs.ToList();
            rStaff.ValueMember = "StaffID";
            rStaff.DisplayMember = "StaffName";
            rStaff.NullText = string.Empty;
            gridDSDoiTuong.RepositoryItems.Add(rStaff);
            colStaffID.ColumnEdit = rStaff;


            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colAccountName, colRoleID, colStaffID});

            foreach (DevExpress.XtraGrid.Columns.GridColumn gc in gridViewDsDoiTuong.Columns)
            {
                gc.FilterMode = DevExpress.XtraGrid.ColumnFilterMode.DisplayText;
                gc.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            }
        }

        public override void LoadData()
        {
            try
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Accounts);
                gridDSDoiTuong.DataSource = DB.Accounts.ToList();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        public override void CreateNew()
        {
            try
            {
                FrmEditUser frm = new FrmEditUser(null);
                frm.RequestChangeInstance += new FrmEditUser.OnRequestChangeInstance(LoadData);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Edit()
        {
            try
            {
                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();
                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    Lists.Account account = (Lists.Account)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (account != null)
                    {
                        FrmEditUser frm = new FrmEditUser(account);
                        frm.RequestChangeInstance += new FrmEditUser.OnRequestChangeInstance(LoadData);
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Delete()
        {
            try
            {
                int[] selectedIndexes = gridViewDsDoiTuong.GetSelectedRows();
                if (selectedIndexes != null && selectedIndexes.Length > 0)
                {
                    int selectedRowIndex = selectedIndexes[0];
                    Lists.Account deletedObj = (Lists.Account)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (deletedObj != null)
                    {
                        if (deletedObj.AccountID == Lists.Account.CurAccount.AccountID)
                        {
                            MessageBox.Show("You don't have permission to delete this account!");
                            return;
                        }
                        if (MessageBox.Show("Are you sure delete " + "'" + deletedObj.AccountName + "'" + "?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                        else
                        {
                            DB.Accounts.DeleteOnSubmit(deletedObj);
                            DB.SubmitChanges();
                            LoadData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show("Cannot delete this user!");
            }
        }
    }
}
